package com.theaf.core.utils;

import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import java.math.BigInteger;
import java.util.*;


/**
 * @Author:songwenke
 * @Description:JSON工具类
 * @Date:create in 14:08 2017/12/1
 * @Modified By:
 **/

public class JsonUtil {
    /**
     * 将对象转换成json字符串
     * @param data
     * @return
     */
    public static String toJSONString(Object data) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
            mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_EMPTY);
            String string = mapper.writeValueAsString(data);
            return string;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将json结果集转化为对象
     * @param jsonData
     * @param beanType
     * @return
     */
    public static <T> T parseObject(String jsonData, Class<T> beanType) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            T t = mapper.readValue(jsonData, beanType);
            return t;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将json数据转换成list
     * @param jsonData
     * @param beanType
     * @return
     */
    public static <T> List<T> parseArray(String jsonData, Class<T> beanType) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        try {
            List<T> list = new ArrayList();
            Iterator<T> iterator = mapper.readValues(mapper.getJsonFactory().createJsonParser(jsonData),beanType);
            while (iterator.hasNext()){
                list.add(iterator.next());
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args){
        Map map = new HashMap();
        map.put("中国人",new String[]{"sfs","2"});
        map.put("中国2人",new BigInteger("12"));
        map.put("中国3人",true);
        String json = "[\n" +
                "       {\n" +
                "           \"id\": 2,\n" +
                "           \"name\": \"广州研发中心\",\n" +
                "           \"parentid\": 1,\n" +
                "           \"order\": 10\n" +
                "       },\n" +
                "       {\n" +
                "           \"id\": 3,\n" +
                "           \"name\": \"邮箱产品部\",\n" +
                "           \"parentid\": 2,\n" +
                "           \"order\": 40\n" +
                "       }\n" +
                "   ]";
        json = json.replaceAll("\\s*","");
        System.out.println(JsonUtil.toJSONString(map));
        Object ob = JsonUtil.parseArray(json,HashMap[].class);
        System.out.println(String.class.getDeclaredFields()[0].getName());
    }
}